/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.uparis10.m2miageclassic.entityManager;

/**
 *
 * @author taisemra
 */

import fr.uparis10.m2miageclassic.entity.DateEtat;
import fr.uparis10.m2miageclassic.entity.Diplome;
import fr.uparis10.m2miageclassic.entity.Domaine;
import fr.uparis10.m2miageclassic.entity.Etat;
import fr.uparis10.m2miageclassic.entity.Mention;
import fr.uparis10.m2miageclassic.entity.Specialite;
import fr.uparis10.m2miageclassic.entity.TypeDiplome;
import fr.uparis10.m2miageclassic.entity.UFRs;
import fr.uparis10.m2miageclassic.interfaces.MaquetteItf;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.*;
import javax.ejb.Stateless;
@Stateless(mappedName="Maquette")
public class MaquetteManager implements MaquetteItf
{
	@PersistenceContext(unitName="MaquetteFormationPU")
	private EntityManager em;
	public MaquetteManager(){
		super();
	}
    @Override
    public Diplome chercherDiplome(){
        return null;
    }
    
    @Override
    public void editDiplome(String numeroDiplome ,String typDip, String ufr, String domaine, String mention, String nomSpecialite, String nomDiplome){
        Diplome diplome = this.getDiplome(Long.parseLong(numeroDiplome));
        Mention mentions = new Mention();
        Specialite specialite = new Specialite();
        specialite.setNom(nomSpecialite);
        Query qDomaine = em.createQuery("Select d FROM Domaine d WHERE d.id = :id ");
        qDomaine.setParameter("id", Long.parseLong(domaine));
        Query qUfr = em.createQuery("Select u FROM UFRs u WHERE u.id = :id ");
        qUfr.setParameter("id", Long.parseLong(ufr));
        UFRs u = (UFRs)qUfr.getSingleResult();
        u.getDomaines().clear();
        u.getDomaines().add((Domaine)qDomaine.getSingleResult());
        diplome.getUfrs().clear();
        diplome.getUfrs().add(u);
        Query qTyp = em.createQuery("Select t FROM TypeDiplome t WHERE t.id = :id ");
        qTyp.setParameter("id", Long.parseLong(typDip));
        diplome.setTypeDiplomes((TypeDiplome)qTyp.getSingleResult());
        Query qEtat = em.createQuery("SELECT e FROM Etat e WHERE e.etat = :etat");
        qEtat.setParameter("etat", "MODIFIE");
        DateEtat dateEtat = new DateEtat();
        dateEtat.setEtat((Etat)qEtat.getSingleResult());
        dateEtat.setDate(new Date());
        dateEtat.setDiplome(diplome);
        diplome.getDates().add(dateEtat);
        mentions.setNom(mention);
        mentions.getSpecialites().add(specialite);
        specialite.setMention(mentions);
        diplome.setMention(mentions);
        diplome.setNom(nomDiplome);
        this.em.merge(diplome);
    }

    @Override
    public void createDiplome(String typDip, String ufr, String domaine, String mention, String nomSpecialite, String nomDiplome) {
        Diplome diplome = new Diplome();
        Mention mentions = new Mention();
        Specialite specialite = new Specialite();
        specialite.setNom(nomSpecialite);
        Query qDomaine = em.createQuery("Select d FROM Domaine d WHERE d.id = :id ");
        qDomaine.setParameter("id", Long.parseLong(domaine));
        Query qUfr = em.createQuery("Select u FROM UFRs u WHERE u.id = :id ");
        qUfr.setParameter("id", Long.parseLong(ufr));
        UFRs u = (UFRs)qUfr.getSingleResult();
        u.getDomaines().add((Domaine)qDomaine.getSingleResult());
        diplome.getUfrs().add(u);
        Query qTyp = em.createQuery("Select t FROM TypeDiplome t WHERE t.id = :id ");
        qTyp.setParameter("id", Long.parseLong(typDip));
        diplome.setTypeDiplomes((TypeDiplome)qTyp.getSingleResult());
        Query qEtat = em.createQuery("SELECT e FROM Etat e WHERE e.etat = :etat");
        qEtat.setParameter("etat", "CREE");
        DateEtat dateEtat = new DateEtat();
        dateEtat.setEtat((Etat)qEtat.getSingleResult());
        dateEtat.setDate(new Date());
        dateEtat.setDiplome(diplome);
        diplome.getDates().add(dateEtat);
        mentions.setNom(mention);
        mentions.getSpecialites().add(specialite);
        specialite.setMention(mentions);
        diplome.setMention(mentions);
        diplome.setNom(nomDiplome);
        this.em.persist(diplome);
    }

    @Override
    public List<TypeDiplome> getLesTypeDiplome() {
       Query requete = em.createQuery("SELECT  t FROM TypeDiplome t");
       return (List<TypeDiplome>)requete.getResultList() ;
    }
   
    @Override
    public List<UFRs> getLesUFRs() {
       Query requete = em.createQuery("SELECT ufr FROM UFRs ufr");
       return (List<UFRs>)requete.getResultList();
       
    }

    @Override
    public List<Domaine> getLesDomaine() {
       Query requete = em.createQuery("SELECT  d FROM Domaine d");
       return (List<Domaine>)requete.getResultList();
    }
    
    @Override
    public Diplome getDiplome(Long id){
        Query q = em.createQuery("SELECT d FROM Diplome d WHERE d.id = :id");
        q.setParameter("id", id);
        return (Diplome)q.getSingleResult();
    }
    
    @Override
    public List<Diplome> getDiplomes(){
        Query q = em.createQuery("SELECT d FROM Diplome d");
        return (List<Diplome>)q.getResultList();
    }
    
}
